home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / net / netamsrc.arc / amiga.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-09  |  13.6 KB  |  792 lines

  1. /*
  2.  * NOTE: Portions of this code was directly derived from the Amiga C-Kermit
  3.  * and is copyrighted by Jack J. Rouse.  Please read the following Copyright
  4.  * notice.  The other code is Copyright (C) 1987, Mikel Matthews and carries
  5.  * the same restriction as those listed below.
  6.  *                                Mikel Matthews
  7. /*
  8.  Author: Jack Rouse
  9.  Contributed to Columbia University for inclusion in C-Kermit.
  10.  Copyright (C) 1986, Jack J. Rouse, 106 Rubin Ct. Apt. A-4, Cary NC 27511
  11.  Permission is granted to any individual or institution to use, copy, or
  12.  redistribute this software so long as it is not sold for profit, provided 
  13.  this copyright notice is retained. 
  14.  
  15.  The file status routines assume all file protection modes are real, instead
  16.  of just delete protection on files and write protection on disks.
  17. */
  18. #include <stdio.h>        /* standard I/O stuff */
  19. #include "exec/types.h"
  20. #include "exec/exec.h"
  21. #include "devices/serial.h"
  22. #include "devices/timer.h"
  23. #include "libraries/dos.h"
  24. #include "libraries/dosextens.h"
  25. #include "intuition/intuition.h"
  26. #include "intuition/intuitionbase.h"
  27. #include "amiga.h"
  28. #include "fcntl.h"
  29. #include "global.h"
  30. #include "iface.h"
  31. #include "smtp.h"
  32. #include "dir.h"
  33. extern    errno;
  34. struct Process *MyProc;
  35. struct CommandLineInterface *MyCLI;
  36. struct FileHandle *Fptr, *Open();    /* used for input */
  37.  
  38. unsigned nasy;        /* number of asyncs avail */
  39.  
  40. /* static definitions */
  41.  
  42. static struct MsgPort *serport;        /* message port for serial comm */
  43. static struct IOExtSer *Read_Request;    /* serial input request */
  44. static struct IOExtSer *Write_Request;    /* serial output request */
  45. static serialopen;
  46.  
  47. /* Exec routines */
  48.  
  49. APTR AllocMem();
  50. struct IORequest *CheckIO();
  51. VOID CloseDevice();
  52. LONG DoIO();
  53. struct Task *FindTask();
  54. struct Message *GetMsg();
  55. LONG OpenDevice();
  56. struct Library *OpenLibrary();
  57. VOID ReplyMsg();
  58. VOID SendIO();
  59. LONG Wait();
  60. LONG WaitIO();
  61. struct Message *WaitPort();
  62.  
  63. /* Exec support */
  64. struct MsgPort *CreatePort();
  65. VOID DeletePort();
  66.  
  67. /* AmigaDOS routines */
  68. BPTR Input();
  69. LONG Read();
  70. LONG WaitForChar();
  71.  
  72. struct asy asy[ASY_MAX];
  73.  
  74. extern x, y, maxcol, MINX, MINY, MAXX;
  75. struct MsgPort *mySerPort;
  76. struct MsgPort *myTimerPort;
  77.  
  78. extern char mailqdir[];
  79.  
  80. struct Store_input {
  81.     char *head;
  82.     char store[MAX_STORE];
  83.     char *tail;
  84.     int    amt;
  85. } Store_input[ASY_MAX];
  86.  
  87. /* Interface list; initialized at compile time to contain
  88.  * just the local interface
  89.  */
  90. struct interface *ifaces;
  91.  
  92. char *ttbuf;
  93.  
  94. char Amiga_drive[255];
  95. char    con_buff[255];
  96. /*
  97.  *  sysinit -- Amiga specific initialization
  98.  */
  99. ioinit() 
  100. {
  101.     register struct IOExtSer *iob;
  102.     char *ptr;
  103.     FILE *ffptr;
  104.  
  105.     /*
  106.      * find my process
  107.      */
  108.  
  109.     MyProc = (struct Process *)FindTask((char *)NULL);
  110.  
  111.     /*
  112.      * where is my cli struct?
  113.      */
  114.  
  115.     MyCLI = (struct CommandLineInterface *)BADDR(MyProc->pr_CLI);
  116.  
  117.     /* now find out where we are */
  118.  
  119.     (void) Execute("cd >RAM:tmpfilename", NULL, NULL);
  120.  
  121.     /*
  122.      *parse the name and place it in the Amiga_drive
  123.      * place holder
  124.      */
  125.     if ( ( ffptr = fopen("RAM:tmpfilename", "r") ) == NULL)
  126.     {
  127.         return;
  128.     }
  129.  
  130.     (void) fgets( Amiga_drive, sizeof(Amiga_drive), ffptr);
  131.     (void) fclose(ffptr);
  132.     (void) unlink("RAM:tmpfilename");
  133.  
  134.     ptr = Amiga_drive;
  135.  
  136.     if ( (ptr = index(Amiga_drive, ':') ) != NULL)
  137.     {
  138.         *ptr = '\0';
  139.     }
  140. }
  141.  
  142.  
  143. iostop(str, val)
  144. char *str;
  145. int val;
  146. {
  147.     /* close everything */
  148.     /*
  149.     printf("%s, error = %d\n\r", str, val);
  150.     */
  151.  
  152.     if (serialopen)
  153.     {
  154.         CloseDevice(Read_Request);
  155.     }
  156.     if ( mySerPort )
  157.     {
  158.         DeletePort(mySerPort);
  159.     }
  160.  
  161. }
  162. char Receive_store[MAX_STORE];
  163. char Output_store[MAX_STORE];
  164.  
  165. asy_init(dev, bufsize)
  166. int dev, bufsize;
  167. {
  168.     register struct asy *ap;
  169.     register struct interface *if_asy;
  170.     extern struct interface *ifaces;
  171.     register struct IOExtSer *iob;
  172.     char *b, *c;
  173.     int i;
  174.     struct Store_input *store;
  175.     iob = Read_Request;
  176.     
  177.     /*
  178.     printf("ASY_INIT: dev = %d\n\r", dev);
  179.     */
  180.     
  181.     ap = &asy[dev];
  182.  
  183.     if ( ap == NULL)
  184.     {
  185.         printf("asy_init: returing -1\n");
  186.         return(-1);
  187.     }
  188.     
  189.  
  190.     if (serialopen)
  191.     {
  192.         printf("asy_init: serial line already open\n");
  193.         return(0);    /* ignore if already open */
  194.     }
  195.  
  196.     Read_Request = (struct IOExtSer *)
  197.         AllocMem((long) sizeof(*Read_Request), MEMF_PUBLIC | MEMF_CLEAR);
  198.  
  199.  
  200.     Read_Request->IOSer.io_Message.mn_ReplyPort = CreatePort(0L, 0L);
  201.  
  202.     if (OpenDevice(SERIALNAME, NULL, Read_Request, NULL))
  203.     {
  204.         iostop("Cant open Read device", 5L);
  205.     }
  206.  
  207.     setmem(&Read_Request->io_TermArray, sizeof(struct IOTArray), 0xc0);
  208.     Read_Request->io_SerFlags = SERF_XDISABLED | SERF_EOFMODE;
  209.     Read_Request->io_Baud = 9600L;
  210.     Read_Request->io_ReadLen = 8L;
  211.     Read_Request->io_WriteLen = 8L;
  212.     Read_Request->io_CtlChar = 0x00;
  213.     Read_Request->io_RBufLen = sizeof(Receive_store);
  214.     Read_Request->io_BrkTime = 0L;
  215.     Read_Request->IOSer.io_Command = SDCMD_SETPARAMS;
  216.  
  217.     DoIO(Read_Request);
  218.  
  219.     Read_Request->IOSer.io_Command = CMD_READ;
  220.     Read_Request->IOSer.io_Length = 1L;
  221.     Read_Request->IOSer.io_Data = (APTR)Receive_store;
  222.  
  223.     Write_Request = (struct IOExtSer *)
  224.         AllocMem((long) sizeof(*Write_Request), MEMF_PUBLIC | MEMF_CLEAR);
  225.  
  226.     b = (BYTE *) Read_Request;
  227.     c = (BYTE *) Write_Request;
  228.  
  229.     for ( i = 0; i < sizeof (struct IOExtSer); i++)
  230.     {
  231.         *c++ = *b++;
  232.     }
  233.  
  234.     Write_Request->io_SerFlags = SERF_XDISABLED;
  235.     Write_Request->IOSer.io_Message.mn_ReplyPort = CreatePort(0L, 0L);
  236.     Write_Request->IOSer.io_Command = CMD_WRITE;
  237.     Write_Request->IOSer.io_Length = 1L;
  238.     Write_Request->IOSer.io_Data = (APTR) Output_store;
  239.  
  240.     store = &Store_input[dev];
  241.     store -> head = store -> tail = store -> store;
  242.     store -> amt = 0;
  243.  
  244.     serialopen = TRUE;
  245.  
  246.     return (0L);
  247.  
  248. }
  249.  
  250. asy_stop(interface)     /* ttclos() */
  251. struct interface *interface;
  252. {
  253.     register struct asy *ap;
  254.  
  255.     if (!serialopen)
  256.     {
  257.         return(0);
  258.     }
  259.     CloseDevice(Read_Request);
  260.     serialopen = FALSE;
  261.     return(0);
  262. }
  263.  
  264.  
  265. asy_ioctl(interface, argc, argv)
  266. struct interface *interface;
  267. int argc;
  268. char *argv[];
  269. {
  270.     if ( argc < 1 ) 
  271.     {
  272.         printf("%d\n", asy[interface->dev].speed);
  273.         return 0;
  274.     }
  275.  
  276.     return ( asy_speed( interface->dev, atoi(argv[0])));
  277. }
  278.  
  279. asy_speed(dev, baud)
  280. int dev, baud;
  281. {
  282.     register struct IOExtSer *iob = Read_Request;
  283.     int    speed;
  284.     int e;
  285.  
  286.     /*
  287.     printf("asy_speed, baud = %d\n\r", baud);
  288.     */
  289.     
  290.     if(baud == 0 || dev >= nasy)
  291.     {
  292.         printf("Baud = %d, dev = %d, returning BAD!!\n", baud, dev);
  293.         return(-1);
  294.     }
  295.     asy[dev].speed = baud;
  296.     /*
  297.     printf("serialopen = %d\n", serialopen);
  298.     */
  299.     if (!serialopen ) 
  300.         return(-1);
  301.  
  302.     /* fill in parameters */
  303.  
  304.     iob->io_CtlChar = 0x1113000L;
  305.  
  306.     if (baud >= 0L && baud <= 19200L) 
  307.     {
  308.         iob->io_Baud = baud;
  309.     }
  310.  
  311.     setmem(&iob->io_TermArray, sizeof(struct IOTArray), 0x12);
  312.  
  313.     iob->io_ReadLen = iob->io_WriteLen = 8L;
  314.     iob->io_StopBits = 1L;
  315.     iob->io_SerFlags = SERF_XDISABLED ;
  316.  
  317.     if ( baud > 19200L)
  318.     {
  319.         iob->io_SerFlags |= SERF_RAD_BOOGIE;
  320.     }
  321.     else
  322.     {
  323.         iob->io_SerFlags &= ~SERF_RAD_BOOGIE;
  324.     }
  325.  
  326.     iob->io_SerFlags &= ~(SERF_PARTY_ON | SERF_EOFMODE);
  327.  
  328.     /* set the parameters */
  329.     
  330.     iob->IOSer.io_Command = SDCMD_SETPARAMS;
  331.  
  332.     DoIO(iob);
  333.     return(0);
  334. }
  335.  
  336.  
  337. asy_output(dev, buf, n)
  338. int dev;
  339. char *buf;
  340. int n;
  341. {
  342.     int err;
  343.     register struct IOExtSer *write = Write_Request;
  344. /*    
  345.     printf("asy_output\n");
  346.     printf("asy_output: dev = %d, count = %d\n", dev, n);
  347.     printf("asy_output: serialopen = %d\n", serialopen);
  348. */    
  349.  
  350.     if (!serialopen) 
  351.     {
  352.         printf("asy_output: returning -1\n");
  353.         return(-1);
  354.     }
  355.     write->IOSer.io_Command = CMD_WRITE;
  356.     write->IOSer.io_Data    = (APTR)buf;
  357.     write->IOSer.io_Length  = (long)n;
  358.     if ( ( err = DoIO(write)) != 0)
  359.     {
  360.         printf("asy_output: err = %d\n", err);
  361.     }
  362.     return(n);
  363. }
  364.  
  365. asy_recv(dev,buf,cnt)
  366. int dev;
  367. char *buf;
  368. unsigned cnt;
  369. {
  370.     register struct IOExtSer *read = Read_Request;
  371.     int tot = 0;
  372.     struct Store_input *store;
  373.     char store_it[MAX_STORE];
  374.     int got = 0;
  375.     int tt, e;
  376.     struct asy *ap;
  377.  
  378. #ifdef DEBUG
  379. printf("asy_recv: dev = %d\n", dev);    
  380. #endif
  381.  
  382.     if ( dev > nasy )
  383.     {
  384.         printf("Error on asy_recv. dev = %d, max = %d\n", dev, nasy);
  385.         return(-1);
  386.     }
  387.  
  388.     ap = &asy[dev];
  389.  
  390.     store = &Store_input[dev];        /* point to current */
  391.  
  392. /* printf("asy_recv: want %d, buffer has %d bytes\n", cnt, store->amt);    */
  393.  
  394.     if ( cnt > 0 && store->amt > 0)
  395.     {
  396.         tt = min(cnt , store->amt);
  397.         tt = min(tt,    MAX_STORE);
  398.         got = 0;
  399.         
  400. /*
  401.  printf ( "(if cnt = %d && store->amt = %d) tt = %d\n", cnt, store->amt, tt);
  402. */
  403.  
  404.         while ( tt-- > 0)
  405.         {
  406.             buf[got++] = *store->tail++;
  407.             if ( store->tail >= &store->store[MAX_STORE])
  408.             {
  409.                 store->tail = store->store;    /* wrap around */
  410.             }
  411.             if ( got >= cnt )
  412.                 break;
  413.         }
  414.         store->amt -= got;
  415.         return( got );
  416.     }
  417.     else if ( cnt == 0 )
  418.     {
  419.         return(0);
  420.     }
  421.  
  422.     /* set up the query */
  423.     read->IOSer.io_Command = SDCMD_QUERY;
  424.  
  425.     /* perform read quickly if possible */
  426.     read->IOSer.io_Flags = IOF_QUICK;
  427.     e = DoIO(read);
  428.     if ( e != 0)
  429.     {
  430.         printf("asy_read: read failed. Error = %d\n", e);
  431.         return(-1);
  432.     }
  433.     if (  (tt = read->IOSer.io_Actual) > 0) 
  434.     {
  435.         /* set up the read */
  436.         read->IOSer.io_Command = CMD_READ;
  437.         read->IOSer.io_Data    = (APTR)ap->recv_buf;
  438.         read->IOSer.io_Length  = tt;
  439.  
  440.         e =  DoIO(read);
  441.  
  442.         if ( e != 0)
  443.         {
  444.             printf("asy_read: read failed. Error = %d\n", e);
  445.             return(-1);
  446.         }
  447.         tt = read->IOSer.io_Actual;
  448.         got = 0;
  449.         while (tt-- > 0)
  450.         {
  451.             *store->head++ = ap->recv_buf[got++];
  452.             if ( store->head == &store->store[MAX_STORE] )
  453.             {
  454.                 /* printf("xx got wrap\n"); */
  455.                 store->head = store->store;
  456.             }
  457.             store->amt++;
  458.         }
  459.         if ( cnt > 0 && store->amt > 0)
  460.         {
  461.             tt = min(cnt , store->amt);
  462.             tt = min(tt,    MAX_STORE);
  463.             got = 0;
  464.         
  465. /*    printf ( "(if cnt = %d && store->amt = %d) tt = %d\n", cnt, store->amt, tt); */
  466.  
  467.             while ( tt-- > 0)
  468.             {
  469.                 buf[got++] = *store->tail++;
  470.                 if ( store->tail >= &store->store[MAX_STORE])
  471.                 {
  472.                     store->tail = store->store;    /* wrap around */
  473.                 }
  474.                 if ( got >= cnt )
  475.                     break;
  476.             }
  477.             store->amt -= got;
  478.             return( got );
  479.         }
  480.         return ( 0 );
  481.     }
  482.     return (got);
  483.  
  484. }
  485.  
  486. long
  487. clksec()
  488. {
  489.     long tloc;
  490.  
  491.     time(&tloc);
  492.     return(tloc);
  493.  
  494. }
  495.  
  496. /* Create a directory listing in a temp file and return the resulting file
  497.  * descriptor. If full == 1, give a full listing; else return just a list
  498.  * of names.
  499.  */
  500. FILE *
  501. dir(path,full)
  502. char *path;
  503. int full;
  504. {
  505.     FILE *fp;
  506.     char cmd[50];
  507.  
  508.     if(full)
  509.         sprintf(cmd,"list >RAM:dir.tmp \"%s\"",path);
  510.     else
  511.         sprintf(cmd,"dir >RAM:dir.tmp \"%s\"",path);
  512.     system(cmd);
  513.     fp = fopen("RAM:dir.tmp","r");
  514.     unlink("RAM:dir.tmp");
  515.     return fp;
  516. }
  517.  
  518. disable()
  519. {
  520.     /* Disable(); */
  521. }
  522.  
  523. restore()
  524. {
  525.     /* Enable(); */
  526. }
  527.  
  528. stxrdy()
  529. {
  530.     return(1);
  531. }
  532.  
  533. Fail(str)
  534. char *str;
  535. {
  536.     char mybuff[255];
  537.     printf("Failure: %s\n", str);
  538. }
  539. eihalt()
  540. {
  541.     FILE *fp;
  542.     return(0);
  543. }
  544. memstat()
  545. {}
  546. int KeepGoing = TRUE;
  547. char
  548. kbread()
  549. {
  550.     char c;
  551.     if ( WaitForChar(Output(), 2) == 0)
  552.     {
  553.         return(-1);
  554.     }
  555.     Read( Input(), &c, 1);
  556.     return(c);
  557. }
  558.  
  559. /* wildcard filename lookup */
  560. static    DIR     *dirp;
  561. static    struct FileInfoBlock *dp;
  562. static     IsOpen = 0;
  563. filedir(name, times, ret_str)
  564. char *name;
  565. int times;
  566. char *ret_str;
  567. {
  568.     char *ptr;
  569.     char *index();
  570.     char *rindex();
  571.     char *name_ptr;
  572.     /*
  573.      * Make sure that the NULL is there incase we don't find anything
  574.      */
  575.     ret_str[0] = NULL;
  576.     if ( ( name_ptr = rindex ( name, '.')) != NULL)
  577.     {
  578.         name_ptr++;
  579.     }
  580.     else
  581.     {
  582.         /*
  583.          * no name to find, so go back
  584.          */
  585.         return;
  586.     }
  587.  
  588.  
  589.     if ( times == 0 && IsOpen == 0 )
  590.     {
  591.         if ( (dirp = opendir(mailqdir)) == NULL ) {
  592.             return;
  593.         }
  594.         IsOpen = 1;
  595.     }
  596.  
  597.     for ( dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
  598.         if ( ( ptr = index( dp ->fib_FileName, '.')) == NULL) {
  599.             continue;
  600.         }
  601.         else {
  602.             ptr++;
  603.             if ( strncmp(ptr, name_ptr, strlen(name_ptr)) != 0) {
  604.                 continue;
  605.             }
  606.             else {
  607.                 sprintf(ret_str,"%s", dp->fib_FileName);
  608.                 break;
  609.             }
  610.         }
  611.     }
  612.     if ( dp == NULL )
  613.     {
  614.         IsOpen = 0;
  615.         closedir(dirp);
  616.     }
  617. }
  618.  
  619.  
  620. /* checks the time then ticks and updates ISS */
  621. static long clkval = 0;
  622. void
  623. check_time()
  624. {
  625.     int32 iss();
  626.  
  627.     if(clkval != clksec()){
  628.         clkval = clksec();
  629.         tick();
  630.         (void)iss();
  631.     }
  632. }
  633.  
  634. memcmp(str1, str2, len)
  635. char *str1, *str2;
  636. int len;
  637. {
  638.     return(strncmp(str1, str2, len));
  639. }
  640.  
  641. memset(str, value, len)
  642. char *str;
  643. int value, len;
  644. {
  645.     while ( len-- !=  0)
  646.     {
  647.         *str++ = value;
  648.     }
  649. }
  650.  
  651. memcpy(str1, str2, len)
  652. char *str1, *str2;
  653. int len;
  654. {
  655.     while ( len-- != 0 )
  656.     {
  657.         *str1++ = *str2++;
  658.     }
  659. }
  660.  
  661. /* Change working directory */
  662. docd(argc,argv)
  663. int argc;
  664. char *argv[];
  665. {
  666.     char dirname[255],*getwd();
  667.  
  668.     if(argc > 1){
  669.         if(chdir(argv[1]) == -1){
  670.             printf("Can't change directory\n");
  671.             return 1;
  672.         }
  673.     }
  674.     if(getwd(dirname,255) != NULLCHAR){
  675.         printf("%s\n",dirname);
  676.     }
  677.     return 0;
  678. }
  679.  
  680. /* List directory to console */
  681. dodir(argc,argv)
  682. int argc;
  683. char *argv[];
  684. {
  685.     char *path;
  686.     char buff[255];
  687.  
  688.     if ( argc > 2)
  689.         sprintf(buff, "dir \"%s\"", argv[1]);
  690.     else
  691.         sprintf(buff, "dir");
  692.  
  693.     Execute(buff, NULL, NULL);
  694.     return 0;
  695. }
  696.  
  697. doshell()
  698. {
  699.     if ( ( Fptr = Open("CON:0/0/640/40/CLI", MODE_NEWFILE) ) == (struct FileHandle *)0L)
  700.     {
  701.         printf("doshell: could not open window.  Error = %d\n", IoErr());
  702.         return;
  703.     }
  704.     if ( Execute("", Fptr, NULL) != -1)
  705.     {
  706.         printf("Could not execute the newcli command.\n");
  707.     }
  708.     
  709.     (void)Close(Fptr);
  710.  
  711.  
  712.     return;
  713. }
  714.  
  715. /*
  716.  * mkdir: make a new directory and set the modes correctly
  717.  */
  718.  
  719. mkdir(name, mode)
  720. char *name;
  721. int mode;
  722. {
  723.     struct FileLock *Lck;    /* file lock on created dir */
  724.     int tmode;
  725.     struct FileLock *CreateDir();
  726.  
  727.     /*
  728.      * if this is successfull a lock is returned
  729.      * and it needs to be unlocked later
  730.      */
  731.     if ( (Lck = CreateDir(name) ) == 0 )
  732.     {
  733.         printf("mkdir: Could not create directory");
  734.         return(-1);
  735.     }
  736.     else
  737.     {
  738.         /* remove the lock */
  739.         UnLock(Lck);
  740.     }
  741.     /*
  742.      * the amiga looks at the modes differently that a unix machine
  743.      */
  744. /*
  745.     tmode = ~(mode << 1);
  746.     if ( SetProtection(name, tmode)  == 0 )
  747.     {
  748.         printf("mkdir: Could not set the modes properly");
  749.         return(-1);
  750.     }
  751. */
  752.     return(0);
  753. }
  754.  
  755. /*
  756.  * rmdir: remove the directory
  757.  */
  758.  
  759. rmdir(name)
  760. char *name;
  761. {
  762.     if ( DeleteFile(name) == 0 )
  763.     {
  764.         printf("Could not delete file");
  765.         return(-1);
  766.     }
  767.     return(0);
  768. }
  769.  
  770. /*
  771.  * this routine will find the first occurance of the chr and return a pointer
  772.  * to it.
  773.  */
  774. char *
  775. memchr(str, chr, cnt)
  776. char *str;
  777. char chr;
  778. int cnt;
  779. {
  780.     int i;
  781.     char *ptr;
  782.  
  783.     for( ptr = str, i = 0; i < cnt; i++, ptr++)
  784.     {
  785.         if (*ptr == chr )
  786.         {
  787.             return(ptr);
  788.         }
  789.     }
  790.     return(NULLCHAR);
  791. }
  792.